<? 
    session_start();
	include ("core/conn.php");
	$db = get_conn();
	check_session();

	$id = $_GET['id'];
	$id_location = $_GET['id_location'];
	
	if($id != ''){
		$sql = " select * from residents  where id_resident='".$id."'"; 
		$result = mysql_query($sql);	
		if($result){
			$row = mysql_fetch_array($result);

			$id_resident = $row['id_resident'];
			$name = $row['name'];
			$lastname = $row['lastname'];
			$lastname2 = $row['lastname2'];
			$id_location = $row['id_location'];
			$id_number = $row['id_number'];
			$telefono = $row['telefono'];
			$email = $row['email'];
			$email2 = $row['email2'];
			$comments = $row['comments'];
			$phone1 = $row['phone1'];
			$phone2 = $row['phone2'];
			$phone3 = $row['phone3'];
			$ext = $row['ext'];
			$workplace = $row['workplace'];
			$ocupation = $row['ocupation'];
			$moving_date = $row['moving_date'];
						
			$id_rol= get_user_rol($id_user);
		}else{
			error_handling(__FUNCTION__.$sql,mysql_error());
		}
	}
	
?>
<html>
<head>
<script>
			<!--
			
				function ver_password(){
					if(document.getElementById('passwd').type == 'text'){
						document.getElementById('passwd').type = 'password'
					}else{
						document.getElementById('passwd').type = 'text'	
					}
				}
				
				function checkdata(obj){
				
							if(obj.name.value ==''){
													
								alert('Debe indicar el Nombre');
													
								obj.name.focus();
													
								return false;
												
							}
												

						


												


												
					return true;
				}
			-->
		</script>
<link href="css/admin_styles.css" rel="stylesheet" type="text/css" />
<script src="tools/jq_dp/js/jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="tools/jq_dp/js/jquery-ui-1.8.11.custom.min.js" type="text/javascript"></script>

<script type="text/javascript" lang="javascript" src="tools/modal/jquery-1.3.2.min.js"></script>
<script src="js/jquery.maskedinput-1.2.2.js"></script>
    <script>
	$(document).ready(function(){
		if($("#phone1")) $("#phone1").mask("****-****");
		if($("#phone2")) $("#phone2").mask("****-****");		
		if($("#phone3")) $("#phone3").mask("****-****");		

		$( "#moving_date" ).datepicker();

		
	}); 
	
	
		
	</script>

</head>
<body>
<form name='users' method='post' action='core/residents.php' onsubmit='return checkdata(this)'>
  <table border='0' width="100%" align="center">
    <tr>
      <td ><div align="center">
        <h2>Residentes</h2>
      </div></td>
    </tr>
    <tr>
      <td class="table_border"><table width='100%' border='0'  >
        <tr>
          <td class="texto">Filial</td>
          <td><? 
		  
		  if($_SESSION['session_id_location'] !=''){
				$sql = "select l.id_location as myvalue, concat(l.number,' | ',l.acc_id) as mydesc  from `locations` l where l.id_location = ".$_SESSION['session_id_location']." order by l.id_location";
		  }else{
				$sql = "select l.id_location as myvalue, concat(l.number,' | ',l.acc_id) as mydesc  from `locations` l order by l.id_location";
		  }
		$js =" tabindex='1' ";
		echo dbcombosql($sql,'id_location',$id_location,$js,$extraitems); ?></td>
          <td class="texto">&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td width="9%" class="texto"><div align="left">Nombre:</div></td>
          <td width="30%"><input  tabindex="1"  name='name' type='text'  id="name" value="<? echo $name;?>" size="40" />
            <input type='hidden' name='id_resident' value='<? echo $id_resident;?>' /></td>
          <td width="6%"><span class="texto">Cedula:</span></td>
          <td width="55%"><input tabindex="4" name='id_number' type='text'  id="id_number" value='<? echo $id_number;?>' size="10" /></td>
        </tr>
        <tr>
          <td class="texto">Apellido:</td>
          <td><input  tabindex="2" name='lastname' type='text'  id="lastname" value="<? echo $lastname;?>" size="40" /></td>
          <td nowrap><span class="texto">Tel&eacute;fono:</span></td>
          <td><input name='phone1' type='text' tabindex="5" id="phone1" value='<? echo $phone1;?>' size="10" /></td>
        </tr>
        <tr>
          <td class="texto">Segundo apellido:</td>
          <td><input  tabindex="3"  name='lastname2' type='text'  id="lastname2" value="<? echo $lastname2;?>" size="40" /></td>
          <td nowrap><span class="texto">Tel&eacute;fono Cell:</span></td>
          <td><input name='phone2' type='text'  id="phone2" tabindex="6" value='<? echo $phone2;?>' size="10" /></td>
        </tr>
        <tr>
          <td colspan="4" nowrap="nowrap" class="texto"><hr size="1"></td>
        </tr>
        <tr>
          <td nowrap="nowrap" class="texto">Lugar de trabajo:</td>
          <td><input name='workplace' type='text' id="workplace" tabindex="35" value='<? echo $workplace;?>' size="40" /></td>
          <td><span class="texto">Ocupacion:</span></td>
          <td><input name='ocupation' type='text' id="ocupation" tabindex="40" value='<? echo $ocupation;?>' size="40" /></td>
        </tr>
        <tr>
          <td nowrap="nowrap" class="texto">Tel&eacute;fono Oficina:</td>
          <td><input name='phone3' type='text' tabindex="45"  id="phone3" value='<? echo $phone3;?>' size="10" /></td>
          <td><span class="texto">Ext</span></td>
          <td><input name='ext' type='text'  tabindex="50" id="ext" value='<? echo $ext;?>' size="10" /></td>
        </tr>
        <tr>
          <td class="texto"><div align="left">E-mail</div></td>
          <td><input  name='email' type='text' value='<? echo $email;?>' size="40" /></td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td class="texto"><div align="left">E-mail Alternativo</div></td>
          <td><input  name='email2' type='text' value='<? echo $email2;?>' size="40" /></td>
          <td colspan="2">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="4" ><hr size="1"></td>
          </tr>
        <tr>
          <td class="texto">Opciones:</td>
          <td colspan="3"><?php echo get_residents_options($id_resident) ?></td>
        </tr>
        <tr>
          <td class="texto">Comentarios</td>
          <td colspan="3"><textarea name="comments" cols="40" rows="3" ><? echo $comments;?></textarea></td>
          </tr>
        <tr>
          <td colspan="4" class="texto"><hr size="1"></td>
        </tr>
        <tr>
          <td colspan='4' align='center'><?
					  if ($id != ""){
						echo "<input type='submit' name='Actualizar' value='Actualizar'>";
						echo "&nbsp;<input type='submit' name='Eliminar' value='Eliminar'>";										
						echo "&nbsp;<input type='submit' name='Nuevo' value='Cancelar'>";
					  }else{
						echo "<input type='submit' name='Agregar' value='Guardar'>";
						echo "&nbsp;<input type='submit' name='Nuevo' value='Cancelar'>";
					  }
					?></td>
        </tr>
      </table></td>
    </tr>
  </table>
</form>
</body>
</html>

<?php
function get_residents_options($id_resident){
	if($id_resident !=''){
		$sql ="select id_option from resident_options_granted where id_resident=".$id_resident;
		$result = mysql_query($sql);
		if($result){
			while($row = mysql_fetch_array($result)){
				$options.=$row['id_option'].",";
			}
			$options = substr($options,0,(strlen($options)-1));
		}else{
			
			error_handling("get_user_options a: ".$sql,mysql_error());
		}
	}
	

	$cols =4;
	$sql ="select * from resident_options ";
	$result = mysql_query($sql);
	if($result){
		while($row = mysql_fetch_array($result)){
			if($counter ==0){
				$data.="<tr>";
			}
			$data.="<td><input type='checkbox' name='id_option[]' value='".$row['id_option']."' id='r_".$row['id_option']."' ".is_in_list($options,$row['id_option'])."><label  class='texto' for='r_".$row['id_option']."'>".$row['name']."</label></td>";
			$counter++;
			if($counter ==$cols){
				$data.="</tr>";
				$counter =0;
			}
		}//while
		return "<table border='0' >".$data."</table>";
	}else{
		error_handling("get_roles: ".$sql,mysql_error());
	}
}

?>